package com.atguigu.spzx.manager.config;

import com.atguigu.spzx.manager.interceptor.LoginAuthInterceptor;
import com.atguigu.spzx.manager.properties.SpzxAuthProperties;
import jakarta.annotation.Resource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableConfigurationProperties(SpzxAuthProperties.class)
public class GuiguWebMvcConfigurer implements WebMvcConfigurer {
    @Resource
    LoginAuthInterceptor loginAuthInterceptor;
    @Resource
    SpzxAuthProperties spzxAuthProperties;
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        WebMvcConfigurer.super.addInterceptors(registry);
//        registry.addInterceptor(new LoginAuthInterceptor())
        registry.addInterceptor(loginAuthInterceptor)
                //拦截路径的白名单
                .excludePathPatterns(spzxAuthProperties.getPaths())
                //拦截请求的路径：可以使用ant风格的url
                .addPathPatterns("/**");
    }
    //统一跨域配置


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") //所有请求
                .allowedOriginPatterns("*") // 允许所有的前端服务器跨域访问
                .allowedMethods("*") //允许所有请求方式跨域访问
                .allowCredentials(true) //允许携带Cookie跨域访问
                .allowedHeaders("*"); //允许携带任意的头跨域访问
    }
}
